import {fileURLToPath, URL} from 'node:url'
import {defineConfig, loadEnv} from 'vite'
import path from 'path'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import {viteMockServe} from 'vite-plugin-mock'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd())
    const {VITE_APP_ENV} = env;
    let esBuild: any = [];
    if (mode === 'production') {
        esBuild = ['console', 'debugger'];
    }

    return {
        base: VITE_APP_ENV === 'production' ? '/' : '/',
        plugins: [
            vue(),
            vueJsx(),
            VueSetupExtend(),
            // mock服务
            viteMockServe({
                logger: true,
                mockPath: "mock",
                //localEnabled: false, // 开发打包开关
                //prodEnabled: false,   // 生产打包开关,这样可以控制关闭mock的时候不让mock打包到最终代码内
            }),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/svg')],
                symbolId: 'icon-[dir]-[name]',
                svgoOptions: command === 'build'
            })
        ],
        resolve: {
            alias: {
                // 设置路径
                '~': path.resolve(__dirname, './'),
                // 设置别名
                '@': fileURLToPath(new URL('./src', import.meta.url))
            },
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
        },
        server: {
            port: 6020,
            host: true,
            open: true
        },
        esbuild: {
            drop: esBuild,
        },
    }
})
